#include "stdafx.h"
#include "InfoBoard.h"

CInfoBoard::CInfoBoard(CDx9Device* aPDevice)
: m_pDevice(aPDevice)
, m_pFont(NULL)
, m_pTextSprite(NULL)
, m_Offset(0)
, m_iFPS(100)
{
	assert(m_pDevice);
	if (FAILED(D3DXCreateFont( m_pDevice->m_pD3DDevice, 15, 0, FW_NORMAL, 1, FALSE, DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
		L"Arial", &m_pFont )))
	{
		TJSMsg(L"failed to Create Font");
	}

	if ( FAILED( D3DXCreateSprite(m_pDevice->m_pD3DDevice, &m_pTextSprite) ) )
	{
		TJSMsg(L"failed to Craete Sprite");
	}
}

CInfoBoard::~CInfoBoard()
{

}

void CInfoBoard::Tick()
{

}

void CInfoBoard::Render()
{
	 m_pTextSprite->Begin( D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE );

	 
	 RECT rt;
	 SetRect(&rt, 0,0,0,10);
	 char buf[512];
	 sprintf_s(buf, "FPS: %d", m_iFPS);
	 m_pFont->DrawTextA(m_pTextSprite, buf, strlen(buf), &rt, DT_NOCLIP, 0xffffff00);

	 int count = 1;
	 for (unsigned int i = m_Offset; i<m_vInfos.size(); i++)
	 {
		  SetRect(&rt, 0,count*15,0,10);
		  m_pFont->DrawTextW(m_pTextSprite, m_vInfos[i].m_strInfo.c_str(), 
			  m_vInfos[i].m_strInfo.length(), &rt, DT_NOCLIP, 0xff00ff00);
		  count++;
	 }

	 for (unsigned int i = 0; i<m_vFrameInfos.size(); i++)
	 {
		 SetRect(&rt, 0,count*15,0,10);
		 m_pFont->DrawTextW(m_pTextSprite, m_vFrameInfos[i].m_strInfo.c_str(), 
			 m_vFrameInfos[i].m_strInfo.length(), &rt, DT_NOCLIP, 0xff0000ff);
		 count++;
	 }

	 m_vFrameInfos.clear();

	 m_pTextSprite->End();
}

void CInfoBoard::PushInfo(wstring aStrInfo)
{
	SInfo info;
	info.m_InfoType = EText;
	info.m_strInfo = aStrInfo;

	m_vInfos.push_back(info);
}

void CInfoBoard::PushInfo(char* aStrFmt, ...)
{
	va_list argptr;
	va_start(argptr, aStrFmt);

	char buf[1024];
	int len = _vsnprintf_s(buf, 1024, aStrFmt, argptr);
	if (len < 0)
	{
		PushInfo(L"overflow!");
		len = 1023;
		return;
	}
	
	wchar_t wbuf[1024];
	wsprintf(wbuf, L"%S", buf);
	PushInfo(wbuf);
	va_end(argptr);
}

void CInfoBoard::PushFrameInfo(D3DXVECTOR3& aVec)
{
	PushFrameInfo("%f, %f, %f", aVec.x, aVec.y, aVec.z);
}

void CInfoBoard::PushFrameInfo(D3DXMATRIX& aMat)
{
	PushFrameInfo("%f, %f, %f, %f", aMat._11, aMat._12, aMat._13, aMat._14);
	PushFrameInfo("%f, %f, %f, %f", aMat._21, aMat._22, aMat._23, aMat._24);
	PushFrameInfo("%f, %f, %f, %f", aMat._31, aMat._32, aMat._33, aMat._34);
	PushFrameInfo("%f, %f, %f, %f", aMat._41, aMat._42, aMat._43, aMat._44);
}

void CInfoBoard::PushFrameInfo(wstring aStrInfo)
{
	SInfo info;
	info.m_InfoType = EText;
	info.m_strInfo = aStrInfo;

	m_vFrameInfos.push_back(info);
}

void CInfoBoard::PushFrameInfo(char* aStrFmt, ...)
{
	va_list argptr;
	va_start(argptr, aStrFmt);

	char buf[1024];
	int len = _vsnprintf_s(buf, 1024, aStrFmt, argptr);
	if (len < 0)
	{
		PushFrameInfo(L"overflow!");
		len = 1023;
		return;
	}

	wchar_t wbuf[1024];
	wsprintf(wbuf, L"%S", buf);
	PushFrameInfo(wbuf);
	va_end(argptr);
}

void CInfoBoard::ProcessMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_MOUSEWHEEL:
		{
			if ((short)HIWORD(wParam) < 0)
			{
				++m_Offset;
				m_Offset = max(0, m_Offset);
			}else
			{
				m_Offset--;
			}
		}
		break;
	}
}